<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="elementtree-qname-objects.html" />
<link rel="prev" href="elementtree-functions.html" />
<link rel="parent" href="module-xml.etree.ElementTree.html" />
<link rel="next" href="elementtree-qname-objects.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>8.13.2 ElementTree Objects</title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="8.13.1 Functions"
  href="elementtree-functions.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="8.13 xml.etree.ElementTree  "
  href="module-xml.etree.ElementTree.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="8.13.3 qname Objects"
  href="elementtree-qname-objects.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="elementtree-functions.html">8.13.1 Functions</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-xml.etree.ElementTree.html">8.13 xml.etree.ElementTree  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="elementtree-qname-objects.html">8.13.3 QName Objects</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h2><a name="SECTION00101320000000000000000"></a><a name="elementtree-elementtree-objects"></a>
<br>
8.13.2 ElementTree Objects
</h2>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-1986' xml:id='l2h-1986' class="class">ElementTree</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>element,</var><big>]</big><var> </var><big>[</big><var>file</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
ElementTree wrapper class.  This class represents an entire element
hierarchy, and adds some extra support for serialization to and from
standard XML.

<p>
<var>element</var> is the root element.
The tree is initialized with the contents of the XML <var>file</var> if given.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1987' xml:id='l2h-1987' class="method">_setroot</tt></b>(</nobr></td>
  <td><var>element</var>)</td></tr></table></dt>
<dd>
Replaces the root element for this tree.  This discards the
current contents of the tree, and replaces it with the given
element.  Use with care.
<var>element</var> is an element instance.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1988' xml:id='l2h-1988' class="method">find</tt></b>(</nobr></td>
  <td><var>path</var>)</td></tr></table></dt>
<dd>
Finds the first toplevel element with given tag.
Same as getroot().find(path).
<var>path</var> is the element to look for.

<p>
<dl><dt><b><tt>Returns:</tt></b></dt>
<dd>
The first matching element, or None if no element was found.
</dd></dl>
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1989' xml:id='l2h-1989' class="method">findall</tt></b>(</nobr></td>
  <td><var>path</var>)</td></tr></table></dt>
<dd>
Finds all toplevel elements with the given tag.
Same as getroot().findall(path).
<var>path</var> is the element to look for.

<p>
<dl><dt><b><tt>Returns:</tt></b></dt>
<dd>
A list or iterator containing all matching elements,
in section order.
</dd></dl>
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1990' xml:id='l2h-1990' class="method">findtext</tt></b>(</nobr></td>
  <td><var>path</var><big>[</big><var>, default</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Finds the element text for the first toplevel element with given
tag.  Same as getroot().findtext(path).
<var>path</var> is the toplevel element to look for.
<var>default</var> is the value to return if the element was not found.

<p>
<dl><dt><b><tt>Returns:</tt></b></dt>
<dd>
The text content of the first matching element, or the
default value no element was found.  Note that if the element
has is found, but has no text content, this method returns an
empty string.
</dd></dl>
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1991' xml:id='l2h-1991' class="method">getiterator</tt></b>(</nobr></td>
  <td><var></var><big>[</big><var>tag</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Creates a tree iterator for the root element.  The iterator loops
over all elements in this tree, in section order.
<var>tag</var> is the tag to look for (default is to return all elements)

<p>
<dl><dt><b><tt>Returns:</tt></b></dt>
<dd>
An iterator.
</dd></dl>
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1992' xml:id='l2h-1992' class="method">getroot</tt></b>(</nobr></td>
  <td><var></var>)</td></tr></table></dt>
<dd>
Gets the root element for this tree.

<p>
<dl><dt><b><tt>Returns:</tt></b></dt>
<dd>
An element instance.
</dd></dl>
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1993' xml:id='l2h-1993' class="method">parse</tt></b>(</nobr></td>
  <td><var>source</var><big>[</big><var>, parser</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Loads an external XML section into this element tree.
<var>source</var> is a file name or file object.
<var>parser</var> is an optional parser instance.  If not given, the
standard XMLTreeBuilder parser is used.

<p>
<dl><dt><b><tt>Returns:</tt></b></dt>
<dd>
The section root element.
</dd></dl>
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-1994' xml:id='l2h-1994' class="method">write</tt></b>(</nobr></td>
  <td><var>file</var><big>[</big><var>, encoding</var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Writes the element tree to a file, as XML.
<var>file</var> is a file name, or a file object opened for writing.
<var>encoding</var> is the output encoding (default is US-ASCII).
</dl>

<p>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="8.13.1 Functions"
  href="elementtree-functions.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="8.13 xml.etree.ElementTree  "
  href="module-xml.etree.ElementTree.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="8.13.3 qname Objects"
  href="elementtree-qname-objects.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="elementtree-functions.html">8.13.1 Functions</a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-xml.etree.ElementTree.html">8.13 xml.etree.ElementTree  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="elementtree-qname-objects.html">8.13.3 QName Objects</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
